import React, { useReducer, useRef } from "react";
import {
  TasksContext,
  tasksReducer,
  initialTasks,
  buildDispatch,
} from "../../redux/index.js";

export const Provider = ({ children }) => {
  const [state, dispatch] = useReducer(tasksReducer, initialTasks);
  const actions = useRef(buildDispatch(dispatch));
  return (
    <TasksContext.Provider
      value={{ state, dispatch, actions: actions.current }}
    >
      {children}
    </TasksContext.Provider>
  );
};
